<html>
<head><meta charset="utf-8"><title>Why did Rust introduce HIR for type checking? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20did.20Rust.20introduce.20HIR.20for.20type.20checking.3F.html">Why did Rust introduce HIR for type checking?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219862722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20did%20Rust%20introduce%20HIR%20for%20type%20checking%3F/near/219862722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20did.20Rust.20introduce.20HIR.20for.20type.20checking.3F.html#219862722">(Dec 14 2020 at 16:08)</a>:</h4>
<p>Is my understanding correct that, HIR was introduced and is being used to:</p>
<ul>
<li>
<p>Be a stable internal compiler representation (Paraphrased from RFC). You can introduce many complicated node types in the AST and then just desugar them into HIR. </p>
</li>
<li>
<p>Simplify the structure of the AST. A use statement and a call path can be combined into a single DefID.</p>
</li>
</ul>
<p>For example in;</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">foo</span>::<span class="n">bar</span><span class="p">;</span><span class="w"></span>

<span class="n">bar</span>::<span class="n">hello</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"></span>
</code></pre></div>
<p>Since every function has a unique DefID, you can simplify the following to something like <code>CallExpr {DefID : 10, args :[x,y]}</code></p>
<ul>
<li>I'm guessing there is something which makes it especially suited to Type checking vs the AST, for batch compilation?</li>
</ul>



<a name="219862867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20did%20Rust%20introduce%20HIR%20for%20type%20checking%3F/near/219862867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20did.20Rust.20introduce.20HIR.20for.20type.20checking.3F.html#219862867">(Dec 14 2020 at 16:09)</a>:</h4>
<p>You may be looking for <a href="https://rustc-dev-guide.rust-lang.org/hir.html?highlight=hir#the-hir">https://rustc-dev-guide.rust-lang.org/hir.html?highlight=hir#the-hir</a></p>



<a name="219863205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20did%20Rust%20introduce%20HIR%20for%20type%20checking%3F/near/219863205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20did.20Rust.20introduce.20HIR.20for.20type.20checking.3F.html#219863205">(Dec 14 2020 at 16:11)</a>:</h4>
<p>Thanks for the link, my question was regarding batch compilation, whereas I believe the link mentions why the HIR would be good for incremental compilation</p>



<a name="219863273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20did%20Rust%20introduce%20HIR%20for%20type%20checking%3F/near/219863273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20did.20Rust.20introduce.20HIR.20for.20type.20checking.3F.html#219863273">(Dec 14 2020 at 16:11)</a>:</h4>
<p>It makes no difference for batch compilation</p>



<a name="219863518"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20did%20Rust%20introduce%20HIR%20for%20type%20checking%3F/near/219863518" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20did.20Rust.20introduce.20HIR.20for.20type.20checking.3F.html#219863518">(Dec 14 2020 at 16:12)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Why.20did.20Rust.20introduce.20HIR.20for.20type.20checking.3F/near/219863273">said</a>:</p>
<blockquote>
<p>It makes no difference for batch compilation</p>
</blockquote>
<p>Ahh right, so it's initial introduction was mostly as a stable internal representation? I thought there was more to the RFC that I might have missed</p>



<a name="219864371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20did%20Rust%20introduce%20HIR%20for%20type%20checking%3F/near/219864371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20did.20Rust.20introduce.20HIR.20for.20type.20checking.3F.html#219864371">(Dec 14 2020 at 16:17)</a>:</h4>
<p>The primary motivation is simplicity, I think</p>



<a name="219878210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20did%20Rust%20introduce%20HIR%20for%20type%20checking%3F/near/219878210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20did.20Rust.20introduce.20HIR.20for.20type.20checking.3F.html#219878210">(Dec 14 2020 at 17:59)</a>:</h4>
<p>It's completely typical when writing a compiler to have several internal representations that each encode a level of detail appropriate for whatever analysis you are doing. For example it's very convenient to have a representation where you have removed syntactic sugar, so that further analysis doesn't have to care about it. In a way it's just a fancy way to avoid code duplication, so in practice you put whatever's convenient in each representation</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>